package com.woniuxy.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户相关的工具类
 */
@Component
public class UserUtils {

    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 得到当前登录用户的LoginUser对象
     * @return
     */
    public LoginUser getLoginUser() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String phone = jwtUtils.getUsername(request.getHeader("token"));
        return (LoginUser) redisTemplate.opsForValue().get("user:" + phone);
    }
}
